package com.njsoft.bodyawakening.photo;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TResult;
import com.njsoft.bodyawakening.R;
import com.njsoft.bodyawakening.event.ImgEventBus;
import com.njsoft.bodyawakening.utils.SPUtileKey;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

public class GetPhotoActivity extends TakePhotoActivity {

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        EventBus.getDefault().post(new ImgEventBus(result.getImage().getOriginalPath()));
        finish();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
        finish();
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
        finish();
    }

    private Uri imageUri;
    private String selectType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        selectType = getIntent().getStringExtra(SPUtileKey.PHOTO_FLAG);
        initUri();
        initTakePhoto();
    }

    private void initUri() {
        File file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        imageUri = Uri.fromFile(file);
    }

    private void initTakePhoto() {
        TakePhoto takePhoto = getTakePhoto();
        switch (selectType) {
            case SPUtileKey.PHOTO_PaiZhao:
                /**
                 * 从相机获取图片并裁剪
                 * @param outPutUri 图片裁剪之后保存的路径
                 * @param options 裁剪配置
                 */
                takePhoto.onPickFromCaptureWithCrop(imageUri, getCropOptions());
                break;
            case SPUtileKey.PHOTO_XiangCe:
                /**
                 * 从相册中获取图片并裁剪
                 * @param outPutUri 图片裁剪之后保存的路径
                 * @param options 裁剪配置
                 */
                takePhoto.onPickFromGalleryWithCrop(imageUri, getCropOptions());
                break;
            default:
        }
    }

    private CropOptions getCropOptions() {


        CropOptions.Builder builder = new CropOptions.Builder();
        builder.setWithOwnCrop(false);
        return builder.create();
    }

}
